package com.dbs.interfaces.group.controller;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;

import javax.jws.soap.SOAPBinding.Use;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.dbs.task.async.AsyncTask;
import com.dbs.task.async.NormalTask;
import com.dbs.task.mail.MailTask;
import com.dbs.interfaces.group.dao.TestDao;
import com.dbs.interfaces.group.entity.User;
import com.dbs.interfaces.group.seconddao.TestSecondDao;
import com.dbs.interfaces.group.service.UserService;

import io.swagger.annotations.ApiOperation;

/**
 * 
 * @author dongbisheng
 * @date Apr 13, 2017 10:31:53 AM
 */
@RestController
@RequestMapping("/users")
public class GroupController {
	// 创建线程安全的Map
	// static Map<Long, User> users = Collections.synchronizedMap(new
	// HashMap<Long, User>());
	@Autowired
	private AsyncTask asyncTask;
	@Autowired
	private NormalTask normalTask;

	@Autowired
	private UserService usrService;

	@Autowired
	private TestDao testDao;

	@Autowired
	private TestSecondDao testSecondDao;

	@Autowired
	private RedisTemplate<String, User> redisTemplate;
	
	/**
	 * 邮件测试
	 * 
	 * @author dongbisheng
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/test/mail")
	public String mailtest() throws Exception {
		MailTask mailTask = new MailTask();
		mailTask.sendSimpleMail();
		/*mailTask.sendAttachmentsMail();
		mailTask.sendInlineMail();*/
		return "mail";
	}

	/**
	 * 统一异常测试
	 * 
	 * @author dongbisheng
	 * @return
	 */
	@RequestMapping(value = "/test/exc", method = RequestMethod.GET)
	// @GetMapping("/test/exc")
	public String testException() throws Exception {
		throw new Exception("异常测试");
	}

	/**
	 * 异步测试
	 * 
	 * @author dongbisheng
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/test/async", method = RequestMethod.GET)
	// @GetMapping("/test/async")
	public String testAsync() throws Exception {
		long start = System.currentTimeMillis();
		Future<String> task1 = asyncTask.doTaskOne();
		Future<String> task2 = asyncTask.doTaskTwo();
		Future<String> task3 = asyncTask.doTaskThree();
		while (true) {
			if (task1.isDone() && task2.isDone() && task3.isDone()) {
				// 三个任务都调用完成，退出循环等待
				break;
			}
			Thread.sleep(1000);
		}
		long end = System.currentTimeMillis();
		System.out.println("任务全部完成，总耗时：" + (end - start) + "毫秒");
		return "异步任务全部完成，总耗时：" + (end - start) + "毫秒" + normalTask.test();
	}

	// @ApiOperation是swagger的參數
	@ApiOperation(value = "获取用户列表", notes = "描述xxxx")
	// @GetMapping("/")
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public List<User> test() {

		return usrService.list();
	}

	// @PostMapping("/")
	@RequestMapping(value = "/", method = RequestMethod.POST)
	public User postUser(@ModelAttribute User user) {
		/** @RequestParamh 和@ModelAttribute类似 一个是属性 一个是类 */
		// 处理"/users/"的POST请求，用来创建User
		// 除了@ModelAttribute绑定参数之外，还可以通过@RequestParam从页面中传递参数
		testDao.save(user);
		testSecondDao.save(user);
		redisTemplate.opsForValue().set(user.getName(), user);
		// return testDao.findByName(user.getName());
		return redisTemplate.opsForValue().get(user.getName());
	}

	// @GetMapping("/{id}")
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public User getUser(@PathVariable Integer id) {
		// 处理"/users/{id}"的GET请求，用来获取url中id值的User信息
		// url中的id可通过@PathVariable绑定到函数的参数中
		return testDao.findOne(id);
	}

	@RequestMapping(value = "/find/{name}", method = RequestMethod.GET)
	public User getUser(@PathVariable String name) {
		// 处理"/users/{id}"的GET请求，用来获取url中id值的User信息
		// url中的id可通过@PathVariable绑定到函数的参数中
		User user = testDao.findByName(name);
		user = testDao.findByName(name);
		user = testDao.findByName(name);
		return user;
	}

	// @DeleteMapping("/{id}")
	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	public String deleteUser(@PathVariable Integer id) {
		// 处理"/users/{id}"的DELETE请求，用来删除User
		testDao.delete(id);
		return "deletesuccess";
	}

	// @PutMapping("/{id}")
	@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
	public String putUser(@PathVariable Integer id, @ModelAttribute User user) {
		// 处理"/users/{id}"的PUT请求，用来更新User信息
		User usernew = testDao.findOne(id);
		if (user.getAge() != null || user.getAge() != 0)
			usernew.setAge(user.getAge());
		// and so on
		testDao.save(usernew);
		return "putsuccess";
	}
}
